package com.nulldev.util.web.nTube.util;

import java.io.File;

import com.nulldev.util.JVM.Arguments;
import com.nulldev.util.data.Variables;
import com.nulldev.util.dataManagement.preferencesAPI.PreferenceInterface;
import com.nulldev.util.dataManagement.preferencesAPI.legacy.LegacyAPIPreferences;
import com.nulldev.util.io.IOUtils;
import com.nulldev.util.io.file.FileUtil;
import com.nulldev.util.languages.json.uniapi.UniJsonValue;
import com.nulldev.util.logging.Logger;
import com.nulldev.util.logging.LoggerFactory;
import com.nulldev.util.web.nTube.NTubeLaunchArguments;
import com.nulldev.util.web.nTube.NTubeServer;

public class NTubeInstanceInfo {

	private static final Logger log = LoggerFactory.getLogger();

	// nTube 1.0 - 1.4.x //

	/**
	 * Vendor for nTube 1.0 - 1.4.x <br>
	 * This includes the LTS releases too.
	 */
	public static final String INSTANCE_VENDOR_LEGACY = "ntube-fossil";
	// nTube 1.5 //

	/**
	 * Vendor for nTube 1.5.x
	 */
	public static final String INSTANCE_VENDOR_1_5_X = "ntube-plastic";
	/**
	 * Vendor for nTube 1.5 alpha/beta/upcoming builds.
	 */
	public static final String INSTANCE_VENDOR_1_5_UPCOMING = "ntube-dino";
	// nTube 1.6 //

	/**
	 * Vendor for nTube 1.6 UI tests.
	 */
	public static final String INSTANCE_VENDOR_1_6_UI_PREP = "ntube-fifth";

	private String instanceName = "nTube-generic";
	private String instanceDescription = "This is a generic instance, either locally or hosted on the internet.";
	private String instanceVersion = NTubeServer.nTube_VERSION.asVersionString();
	private String instanceVendor = INSTANCE_VENDOR_1_5_X;
	private String instanceAuthor = "N/A";
	private String instanceAuthorMail = "N/A";

	public String getInstanceName() {
		return this.instanceName;
	}

	public String getInstanceDescription() {
		return this.instanceDescription;
	}

	public String getInstanceVersion() {
		return this.instanceVersion;
	}

	public String getInstanceVendor() {
		return this.instanceVendor;
	}

	public String getInstanceAuthor() {
		return this.instanceAuthor;
	}

	public String getInstanceAuthorMail() {
		return this.instanceAuthorMail;
	}

	public UniJsonValue asObject() {
		final UniJsonValue obj = UniJsonValue.object(6);
		obj.add("instanceName", this.instanceName);
		obj.add("instanceDescription", this.instanceDescription);
		obj.add("instanceVersion", this.instanceVersion);
		obj.add("instanceVendor", this.instanceVendor);
		obj.add("instanceAuthor", this.instanceAuthor);
		obj.add("instanceAuthorMail", this.instanceAuthorMail);
		return obj;
	}

	public static void parseAndSet(final Arguments.ParsedArguments parsedArguments, final INTubeInstance instance) {
		Variables.requireNonNull(instance, "instance");
		final NTubeInstanceInfo nti = instance.getInstanceInfo();
		if (!parsedArguments.has(NTubeLaunchArguments.nTubeArguments.INSTANCE_INFO_PATH)) {
			log.info("No path was provided for the instance info, skipping.");
			return;
		}
		final String path = parsedArguments.getValue(NTubeLaunchArguments.nTubeArguments.INSTANCE_INFO_PATH);
		if (path == null || path.isEmpty()) {
			log.warn("Failed to read instance info, invalid path.");
			return;
		}
		final File file = FileUtil.safeCreate(path);
		if (file == null || !file.exists() || file.isDirectory()) {
			log.warn("Failed to read instance info, file not found.");
			return;
		}
		final PreferenceInterface<String> pi = new LegacyAPIPreferences(file);
		if (pi.has("instance.name"))
			nti.instanceName = pi.get("instance.name");
		if (pi.has("instance.description"))
			nti.instanceDescription = pi.get("instance.description");
		if (pi.has("instance.version"))
			nti.instanceVersion = pi.get("instance.version");
		if (pi.has("instance.vendor"))
			nti.instanceVendor = pi.get("instance.vendor");
		if (pi.has("instance.author"))
			nti.instanceAuthor = pi.get("instance.author");
		if (pi.has("instance.contact"))
			nti.instanceAuthorMail = pi.get("instance.contact");
		IOUtils.closeQuietly(pi);
		log.info("Loaded instance info.");
	}
}
